package com.xu.demo.common.controller;

import com.alibaba.fastjson.JSONObject;
import com.xu.demo.common.components.RedisTemplateUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

@Slf4j
@Tag(name = "Redis接口")
@RestController
@RequestMapping("/redis")
public class RedisController {

    @Autowired
    private RedisTemplateUtil redisTemplateUtil;

    @Operation(summary ="设置缓存")
    @GetMapping("/setValue")
    public String setValue() {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String key = formatter.format(LocalDateTime.now());
        redisTemplateUtil.set(key, key);
        return key;
    }

    @Operation(summary ="获取缓存")
    @GetMapping("/getValue")
    public Object getValue(@Parameter String key) {
        Object value = redisTemplateUtil.get(key);
        log.info("缓存对象是:{}",JSONObject.toJSONString(value));
        return value;
    }

}